# 截图宝 - 三维动画截图软件主窗体
# 作者： 魏新崇
# 邮箱： weixinchong [at] 163.com
# 项目主页：https://gitee.com/weixinchong/auto-igrab

import sys, pyautogui, os
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QGridLayout, QLabel, QPushButton, QSpinBox, QLineEdit, QDoubleSpinBox, QProgressBar
from PyQt5.QtGui import QFont
from PIL import ImageGrab
from win_base import Window_Base

class View3DWindow(Window_Base):

    def __init__(self, parent = None):
        super(View3DWindow, self).__init__('三维动画截图宝', (167, 131, 696, 594), parent)

        # 在基本空间的基础上添加控件
        # 1.鼠标旋转控制
        # 1.1 创建控件
        label_mouse = QLabel('鼠标旋转控制')
        label_mouse.setFont(self.font_title)
        label_mouse_pos = QLabel('起始坐标：')
        self.spin_mouse_x = self.create_spin(423)
        self.spin_mouse_y = self.create_spin(419)
        btn_mouse_pos_adjust = QPushButton('重定位')
        btn_mouse_pos_adjust.clicked.connect(lambda: self.get_position(self.spin_mouse_x, self.spin_mouse_y))
        label_move_interval = QLabel('偏移时长：')
        label_move_interval_unit = QLabel('秒')
        self.spin_move_interval = QDoubleSpinBox()
        self.spin_move_interval.setValue(0.2)
        self.spin_move_interval.setDecimals(3)
        label_offset = QLabel('偏移量：')
        self.spin_offset_x = self.create_spin(-5, 1000, -1000)
        self.spin_offset_y = self.create_spin(0, 1000, -1000)
        btn_offset_test = QPushButton("单步调试")
        btn_offset_test.clicked.connect(self.one_step_test)
        # 1.2 控件导入布局
        self.grid.addWidget(label_mouse, 4, 0, 1, 4)
        self.grid.addWidget(label_mouse_pos, 5, 0)
        self.grid.addWidget(self.spin_mouse_x, 5, 1)
        self.grid.addWidget(self.spin_mouse_y, 5, 2)
        self.grid.addWidget(btn_mouse_pos_adjust, 5, 3)
        self.grid.addWidget(label_move_interval, 6, 0)
        self.grid.addWidget(self.spin_move_interval, 6, 1, 1, 2)
        self.grid.addWidget(label_move_interval_unit, 6, 3)
        self.grid.addWidget(label_offset, 7, 0)
        self.grid.addWidget(self.spin_offset_x, 7, 1)
        self.grid.addWidget(self.spin_offset_y, 7, 2)
        self.grid.addWidget(btn_offset_test, 7, 3)

        # 2.输出控制
        # 2.1 创建控件
        label_output = QLabel('输出控制')
        label_output.setFont(self.font_title)
        label_count = QLabel('旋转次数：')
        label_count_unit = QLabel('次')
        self.spin_count = self.create_spin(50, vmin = 1)
        label_fps = QLabel('动画帧率：')
        label_fps_unit = QLabel('fps')
        self.spin_fps = self.create_spin(20, 60, 1)
        label_pic = QLabel('保存图片：')
        self.dir_pic = QLineEdit()
        self.dir_pic.setText(self.cwd + '/pics')
        btn_sel_pic = QPushButton('…')
        btn_sel_pic.setMinimumWidth(35)
        btn_sel_pic.clicked.connect(self.sel_pic_path)
        btn_clear_pic = QPushButton('×')
        btn_clear_pic.setMinimumWidth(35)
        btn_clear_pic.clicked.connect(lambda: self.dir_pic.clear())
        label_video = QLabel('输出动画：')
        self.dir_video = QLineEdit()
        self.dir_video.setText(self.cwd + '/animate.mp4')
        btn_sel_video = QPushButton('…')
        btn_sel_video.setMinimumWidth(35)
        btn_sel_video.clicked.connect(self.sel_video_path)
        btn_clear_video = QPushButton('×')
        btn_clear_video.setMinimumWidth(35)
        btn_clear_video.clicked.connect(lambda: self.dir_video.clear())
        # 2.2 控件导入布局
        self.grid.addWidget(label_output, 8, 0, 1, 4)
        self.grid.addWidget(label_count, 9, 0)
        self.grid.addWidget(self.spin_count, 9, 1, 1, 2)
        self.grid.addWidget(label_count_unit, 9, 3)
        self.grid.addWidget(label_fps, 10, 0)
        self.grid.addWidget(self.spin_fps, 10, 1, 1, 2)
        self.grid.addWidget(label_fps_unit, 10, 3)
        self.grid.addWidget(label_pic, 11, 0)
        self.grid.addWidget(self.dir_pic, 11, 1, 1, 2)
        sublay_pic = QHBoxLayout()
        sublay_pic.setSpacing(4)
        sublay_pic.addWidget(btn_sel_pic)
        sublay_pic.addWidget(btn_clear_pic)
        self.grid.addLayout(sublay_pic, 11, 3)
        self.grid.addWidget(label_video, 12, 0)
        self.grid.addWidget(self.dir_video, 12, 1, 1, 2)
        sublay_video = QHBoxLayout()
        sublay_video.setSpacing(4)
        sublay_video.addWidget(btn_sel_video)
        sublay_video.addWidget(btn_clear_video)
        self.grid.addLayout(sublay_video, 12, 3)
        
        # 4.截图按钮
        # 4.1 创建控件
        btn_test = QPushButton('测试动画效果')
        btn_run = QPushButton('正式截图及合成')
        btn_test.clicked.connect(self.test)
        btn_run.clicked.connect(self.run)
        self.label_state = QLabel('【待运行】')
        self.progress_bar = QProgressBar()
        self.progress_bar.setValue(0)
        # 4.2 控件导入布局
        self.grid.addWidget(btn_test, 13, 0, 1, 2)
        self.grid.addWidget(btn_run, 13, 2, 1, 2)
        self.grid.addWidget(self.label_state, 14, 0)
        self.grid.addWidget(self.progress_bar, 14, 1, 1, 3)

    def get_params(self):
        '''读取用户输入的参数值'''
        super(View3DWindow, self).get_params()
        self.mouse_x = self.spin_mouse_x.value()
        self.mouse_y = self.spin_mouse_y.value()
        self.move_interval = self.spin_move_interval.value()
        self.x_offset = self.spin_offset_x.value()
        self.y_offset = self.spin_offset_y.value()
        self.img_count = self.spin_count.value()
        self.fps = self.spin_fps.value()

    def adjust_angle(self):
        '''调整角度'''
        pyautogui.mouseDown(x = self.mouse_x, y = self.mouse_y, button = 'left')  #按下鼠标
        pyautogui.dragTo(x = self.mouse_x + self.x_offset, y = self.mouse_y + self.y_offset, duration = self.move_interval, button = 'left')  #鼠标拖拽
        pyautogui.mouseUp(x = self.mouse_x + self.x_offset, y = self.mouse_y + self.y_offset, button = 'left')  #松开鼠标

    def one_step_test(self):
        '''调整角度的单步测试'''
        self.get_params()
        x0, y0 = pyautogui.position()
        self.adjust_angle()
        pyautogui.moveTo(x0, y0)

    def test(self):
        '''截图测试'''
        self.get_params()
        self.label_state.setText('【测试中】')
        self.progress_bar.setValue(0)
        x0, y0 = pyautogui.position()
        for i in range(self.img_count):
            self.adjust_angle()
            percent = int((i + 1) / self.img_count * 100)
            if percent > self.progress_bar.value():
                self.progress_bar.setValue(percent)
            QApplication.processEvents()
        pyautogui.moveTo(x0, y0)
        self.label_state.setText('【测试终】')

    def run(self):
        '''进行截图操作'''
        self.get_params()
        self.label_state.setText('【截图中】')
        self.progress_bar.setValue(0)
        x0, y0 = pyautogui.position()
        create_video = self.dir_video.text() != ''
        save_pic = self.dir_pic.text() != ''
        if not save_pic:
            fPath = self.cwd
        else:
            fPath = self.dir_pic.text()
        if create_video:
            self.init_video(self.dir_video.text())
        for i in range(self.img_count):
            fName = '%s/p%03d.jpg' % (fPath, i + 1)
            self.window_capture(fName)
            if create_video:
                self.append_frame(fName)
            if not save_pic:
                os.remove(fName)
            self.adjust_angle()
            percent = int((i + 1) / self.img_count * 100)
            if percent > self.progress_bar.value():
                self.progress_bar.setValue(percent)
            QApplication.processEvents()
        if create_video:
            self.end_video(self.dir_video.text())
        pyautogui.moveTo(x0, y0)
        self.label_state.setText('【截图终】')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    v3d_win = View3DWindow()
    v3d_win.show()
    sys.exit(app.exec_())